#ifndef WXSCODER_H
#define WXSCODER_H

#include <wx/string.h>
#include <wx/thread.h>
#include <map>
#include <cbeditor.h>
#include <time.h>
#include "wxsglobals.h"


#define WXS_CODE_WAIT_TIME	3	///< Time in sec between putting the code into queue and merging it with real files, currently not used


/** This class is responsible for managing code created on-the-fly
 *
 * Current implementation does not shedule coded upgrades which may cause
 * bad results in performance.
 */
class wxsCoder
{
	public:

		/** Ctor */
		wxsCoder();

		/** Dctor */
		virtual ~wxsCoder();

		/** Function adding block of code to processing queue
		 *
		 * Code should use tabs for indentation and will be automatically
		 * indented relatively to code header
		 */
		void AddCode(const wxString& FileName,const wxString& BlockHeader,const wxString& Code,bool Immediately = true);

		/** Function reading block of code from given source file */
		wxString GetCode(const wxString& FileName,const wxString& BlockHeader);

		/** Function processing all code requests waiting insided queue */
		bool ProcessCodeQueue();

		/** Function dropping all code requests from queue */
		void DropQueue();

		/** Function processing all requests for given file */
		bool ProcessCodeForFile(const wxString& FileName);

		/** Function getting singleton object from system */
		static wxsCoder* Get() { return Singleton; }

	private:

		/** Structure describing one code block */
		struct CodeEntry
		{
			wxString FileName;			///< Name of file
			wxString BlockHeader;		///< Header of auto-generated code
			wxString Code;				///< Code
			CodeEntry* Next;			///< Next code in the list
			time_t TimeStamp;			///< Timestamp of lastest update
		};

		/** List of all enteries */
		CodeEntry* Enteries;

		/** Function applying changed to project file.
		 *
		 * If the file is opened inside editor, editor's buffer will be modified
		 * and midified flag will be set. If file is not open, the real file will
		 * be changed
		 */
        bool ApplyChanges(CodeEntry* Entry);

		/** Function applying hanges to currently opened editor */
		bool ApplyChanges(CodeEntry* Entry, cbEditor* Editor);

		/** Function applying changes to file which is not open inside editor */
		bool ApplyChanges(CodeEntry* Entry, const wxString& FileName);

        /** Rebuilding code to support current editor settings */
        void RebuildCode(wxString& BaseIndentation,wxString& Code);

        /** Cutting off given number of spaces at every new line */
        wxString CutSpaces(wxString Code,int Count);

		/** Singleton object */
		static wxsCoder* Singleton;

		/** Mutex for this object - added in case of multi-threading shedules */
		wxMutex DataMutex;

		/** Variable used to blocking processing of data */
		bool BlockProcess;
};

/** \page Auto-Code Code automatically generated by wxSmith
 *
 * Here's list of automatically generated code:
 *
 * \li \c //(*EventTable($CLASSNAME) - generated in class source file, contains
 *                                  enteries for event table
 * \li \c //(*Initialize($CLASSNAME) - generated in class source file, this code
 *                                  does all resource initialization (loading
 *                                  XRC, adding widgets etc.)
 * \li \c //(*Headers($CLASSNAME) - generated in class header file, this block
 *                                  contains set of #includes including required
 *                                  header files for this resource
 * \li \c //(*Identifiers($CLASSNAME) - generated in class header file, this
 *                                  code generates identifiers for window
 *                                  items (usually enum), in case of XRC
 *                                  resource (with it's own identifier
 *                                  handling system), it will be empty
 * \li \c //(*Handlers($CLASSNAME) - generated in class header. It contains
 *                                  declarations of event handler functions,
 *                                  inside this code, user may put it's own
 *                                  event handler declarations but they must
 *                                  be in form:
 *                                     void HandlerName(eventType& event).
 *                                  This block is parsed when generating list
 *                                  of event handlers which may be used with
 *                                  given event type.
 * \li \c //(*Declarations($CLASSNAME) - declarations of window items. This
 *                                  block will contain declarations of all
 *                                  window items which have "Is Member"
 *                                  property set to true.
 *
 * Blocks which will be added in future:
 *
 * \li \c //(*AppHeaders - declared in main application's source file.
 *                                 This block will contain set of #includes
 *                                 required by application.
 * \li \c //(*AppInitialize - declared in main application's source file.
 *                                 This block will automatically load resources
 *                                 and show main application's window
 */

#endif // WXSCODER_H
